//
//  TYZNoDataOrErrorView.m
//  DriveSchoolCoach
//
//  Created by TangBin on 2019/7/4.
//  Copyright © 2019 TangBin. All rights reserved.
//

#import "TYZNoDataOrErrorView.h"

@interface TYZNoDataOrErrorView ()
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, copy) NSString *tip;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *tipLabel;
@property (nonatomic, strong) UILabel *descLabel;
@property (nonatomic, strong) UIButton *btnReset; ///< 重新加载

- (void)initWithImageView;

- (void)initWithTipLabel;

- (void)initWithBtnReset;
@end

@implementation TYZNoDataOrErrorView

- (void)dealloc
{
    
}

//- (instancetype)initWithFrame:(CGRect)frame withImage:(UIImage *)image withTip:(NSString *)tip tapGestureBlock:(void (^)(id))block
//{
//    self.image = image;
//    self.tip = tip;
//    self.viewBlock = block;
//    return [super initWithFrame:frame];
//}

- (void)initWithVar
{
    
}

- (void)initWithSubView
{
    [self initWithImageView];
    
    [self initWithTipLabel];
    
    [self initWithDescLabel];
    
    [self initWithBtnReset];
}

- (void)initWithImageView
{
    if (!_imageView)
    {
        UIImage *image = [UIImage imageNamed:@"ic_network_failed"];
//        CGRect frame = CGRectMake((self.width - image.size.width) * 0.5, (self.height - image.size.height) * 0.5 - 20, image.size.width, image.size.height);
        CGRect frame = CGRectMake((self.width - image.size.width) * 0.5, 0, image.size.width, image.size.height);
        _imageView = [[UIImageView alloc] initWithFrame:frame];
        _imageView.image = (_image==nil?nil:_image);
        _imageView.userInteractionEnabled = YES;
//        _imageView.backgroundColor = [UIColor redColor];
        [self addSubview:_imageView];
    }
}



- (void)initWithTipLabel
{
    if (!_tipLabel)
    {
                CGRect frame = CGRectMake(15, _imageView.bottom + 5, self.width - 30, 20);
//        CGRect frame = CGRectMake(0, _imageView.height - 20- 23, _imageView.width, 20);
        _tipLabel = [[UILabel alloc] initWithFrame:frame];
        _tipLabel.textAlignment = NSTextAlignmentCenter;
        _tipLabel.textColor = [UIColor colorWithHexString:@"#666666"];
        _tipLabel.font = [UIFont systemFontOfSize:kFontCustSize(16)];
        _tipLabel.userInteractionEnabled = YES;
//        [_imageView addSubview:_tipLabel];
        [self addSubview:_tipLabel];
    }
}

- (void)initWithDescLabel
{
    if (!_descLabel)
    {
        CGRect frame = CGRectMake(15, _tipLabel.bottom + 5, self.width - 30, 20);
        _descLabel = [[UILabel alloc] initWithFrame:frame];
        _descLabel.textAlignment = NSTextAlignmentCenter;
        _descLabel.textColor = [UIColor colorWithHexString:@"#666666"];
        _descLabel.font = [UIFont systemFontOfSize:kFontCustSize(14)];
        _descLabel.userInteractionEnabled = YES;
        [self addSubview:_descLabel];
    }
}

- (void)initWithBtnReset
{
    if (!_btnReset)
    {
        float width = KLineX(130);
        float height = KLineY(36);
        CGRect frame = CGRectMake((self.width - width) * 0.5, self.height - 40, width, height);
        _btnReset = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnReset.frame = frame;
        _btnReset.titleLabel.font = [UIFont boldSystemFontOfSize:kFontCustSize(15)];
        [_btnReset setTitle:@"点击重试" forState:UIControlStateNormal];
        [_btnReset setTitleColor:kThemeColor forState:UIControlStateNormal];
        _btnReset.backgroundColor = [UIColor whiteColor];
        _btnReset.layer.cornerRadius = frame.size.height * 0.5;
        _btnReset.layer.masksToBounds = YES;
        _btnReset.layer.borderColor = kThemeColor.CGColor;
        _btnReset.layer.borderWidth = 1;
        [_btnReset addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_btnReset];
        
    }
}

- (void)clicked:(id)sender
{
    [self tapGesture:nil];
}

- (void)tapGesture:(UITapGestureRecognizer *)tap
{
    if (self.viewBlock)
    {
        self.viewBlock(nil);
    }
}

- (void)updateWithImage:(UIImage *)image withTip:(NSString *)tip tipFont:(nonnull UIFont *)tipFont tipColor:(nonnull UIColor *)tipColor btnTop:(CGFloat)btnTop btnResetHidden:(BOOL)hidden descHidden:(BOOL)descHidden
{
    self.userInteractionEnabled = !hidden;
    self.image = image;
    self.tip = tip;
    _imageView.image = self.image;
    _imageView.size = image.size;
    _imageView.left = (self.width - image.size.width) * 0.5;
    _tipLabel.text = tip;
    _tipLabel.font = tipFont;
    _btnReset.hidden = hidden;
    _descLabel.hidden = YES;
    _tipLabel.top = _imageView.bottom + 35;
    _btnReset.top = _tipLabel.bottom + btnTop;
    if (!descHidden) {
        _descLabel.top = _tipLabel.bottom + 6;
        _btnReset.top = _descLabel.bottom + btnTop;
    }
    
    self.height = _btnReset.bottom + 10;
}

- (void)updateWithImage:(UIImage *)image withTip:(NSString *)tip btnResetHidden:(BOOL)hidden descHidden:(BOOL)descHidden
{
    [self updateWithImage:image withTip:tip tipFont:self.tipLabel.font tipColor:self.tipLabel.textColor btnTop:10 btnResetHidden:hidden descHidden:descHidden];
}

- (void)updateImageSize:(CGSize)size
{
    _imageView.size = size;
//    _imageView.backgroundColor = [UIColor redColor];
    _imageView.left = (self.width - size.width) * 0.5;
    
    _tipLabel.top = _imageView.bottom + 35;
    _btnReset.top = _tipLabel.bottom + 10;
    
    self.height = _btnReset.bottom + 10;
}

- (void)updateWithBtnReset:(NSString *)title backgroundColor:(UIColor *)backgroundColor fontColor:(UIColor *)fontColor
{
    self.btnReset.hidden = NO;
    [_btnReset setTitle:title forState:UIControlStateNormal];
    [self.btnReset setTitleColor:fontColor forState:UIControlStateNormal];
    self.btnReset.backgroundColor = backgroundColor;
}

- (void)updateWithDesc:(NSString *)desc font:(UIFont *)font textColor:(UIColor *)textColor
{
    self.descLabel.hidden = NO;
    self.descLabel.font = font;
    self.descLabel.text = desc;
    self.descLabel.textColor = textColor;
    if (self.btnReset.hidden == NO) {
        self.btnReset.top = self.height - self.btnReset.height;
    }
}

@end
